package dhm.com.hpx.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.SpeechcraftListAdapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.entity.CatLListBean;
import dhm.com.hpx.utils.SpUtils;

public class SpeechcraftlistActivity extends BaseActivity implements LoginContract.IView {


    @BindView(R.id.edit)
    RecyclerView edit;

    private PressenterImpl pressenter;
    private String uid;
    private SpeechcraftListAdapter adapter;
    private CatLListBean catLListBean;

    @Override
    protected int getLayout() {
        return R.layout.activity_speechcraftlist;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(this, "uid");

        GridLayoutManager manager=new GridLayoutManager(this,4);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        edit.setLayoutManager(manager);

        adapter = new SpeechcraftListAdapter(this);
        adapter.setdeleteClick(new SpeechcraftListAdapter.OnClick() {
            @Override
            public void item(int posttion) {
                Map<String,String> map=new HashMap<>();
                map.put("token",Constant.TOKEN);
                map.put("uid",uid);
                map.put("cat_id",catLListBean.getData().get(posttion).getCat_id()+"");
                pressenter.sendMessage(SpeechcraftlistActivity.this,Constant.del_cat,map, Bean.class);
            }
        });
        edit.setAdapter(adapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getdata();
    }

    private void getdata() {
        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        pressenter.sendMessage(this,Constant.cat_list,map, CatLListBean.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {
        if (data instanceof CatLListBean){
            catLListBean = (CatLListBean) data;
            if (catLListBean.getCode()==1){
                CatLListBean.DataBean bean=new CatLListBean.DataBean();
                bean.setCat_name("+自定义");
                catLListBean.getData().add(bean);
                adapter.setList(catLListBean.getData());
            }
        }if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode()==1){
                getdata();
            }
        }
    }

    @Override
    public void fail(String error) {

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
